//
//  CopySafeWordController.m
//  SafPay
//
//  Created by 贺庆博 on 2018/6/19.
//  Copyright © 2018年 ebo. All rights reserved.
//

#import "CopySafeWordController.h"
#import "SafWordCell.h"
#import "SureSafeWordController.h"
@interface CopySafeWordController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong,nonatomic) UIView *mainView;
@property (strong,nonatomic) UICollectionView *myCollectionView;
@property (strong,nonatomic) NSMutableArray *arrDS;

@end

@implementation CopySafeWordController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"备份助记词";
    // Do any additional setup after loading the view from its nib.
    [self getWord];
    [self initUI];
    

}
- (UIView*)mainView
{
    if (!_mainView) {
        _mainView = [UIView new];
        _mainView.backgroundColor = [UIColor whiteColor];
        UILabel *titleLab = [UILabel labelWithFont:kFONT(18) color:rgb(58,58,58) text:@"抄写下你的钱包助记词"];
        titleLab.textAlignment = NSTextAlignmentCenter;
        [_mainView addSubview:titleLab];
        [_mainView addSubview:self.myCollectionView];
        UILabel *subLab = [UILabel labelWithFont:kFONT(15) color:rgb(159,159,159) text:@"助记词用于恢复钱包或充值钱包密码，将它准确的抄写到纸上，并存放在只有你知道的安全地方。"];
        subLab.numberOfLines = 0;
        [_mainView addSubview:subLab];
        __weak typeof (UIView *) weakView = _mainView;
        __weak typeof (UICollectionView *) weakCollection = _myCollectionView;

        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(48);
            make.left.mas_equalTo(46);
            make.right.equalTo(weakView).offset(-46);
        }];
        
        [_myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(titleLab.mas_bottom).mas_offset(42);
            make.centerX.equalTo(weakView);
            make.height.equalTo(@142);
            make.width.equalTo(@298);

        }];
        
        [subLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(weakCollection.mas_bottom).mas_offset(46);
            make.left.mas_equalTo(46);
            make.right.equalTo(weakView).offset(-46);
        }];
    }
    return _mainView;
}

-(UICollectionView *)myCollectionView{
    if(!_myCollectionView){
        
        //创建一个layout布局类
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        //设置布局方向为垂直流布局
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        //设置每个item的大小为100*100
        layout.itemSize = CGSizeMake(53, 27);
        //创建collectionView 通过一个布局策略layout来创建
        _myCollectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
        //代理设置
        _myCollectionView.layer.cornerRadius = 8;
        _myCollectionView.layer.borderColor = rgb(186,186,186).CGColor;
        _myCollectionView.layer.borderWidth = 1;
        _myCollectionView.delegate=self;
        _myCollectionView.dataSource=self;
        //注册item类型 这里使用系统的类型
        [_myCollectionView registerNib:[UINib nibWithNibName:@"SafWordCell" bundle:nil] forCellWithReuseIdentifier:@"SafWordCell"];
        [_myCollectionView setBackgroundColor:[UIColor clearColor]];
        [_myCollectionView setScrollEnabled:NO];
        
    }
    return _myCollectionView;
}

- (NSMutableArray *)arrDS
{
    if (!_arrDS) {
        _arrDS = [NSMutableArray array];
     
    }
    return _arrDS;
}
- (void)initUI
{
    
  
    [self.view addSubview:self.mainView];
    [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(22, 12, 66, 12));
    }];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"下一步" forState:UIControlStateNormal];
    btn.titleLabel.font = kFONT(16);
    btn.backgroundColor = rgb(33,177,227);
    [self.view addSubview:btn];
    kWeakSelf(weakSelf);
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        SureSafeWordController *vc = [SureSafeWordController new];
//        vc.sureKeyWord= weakSelf.arrDS;
//        vc.isHome=self.isHome;
//        vc.isSetting=self.isSetting;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.height.equalTo(@44);
    }];
}
-(void)getWord{
    
    if(self.keyWord.length!=0){
        
        NSArray *arr=[self.keyWord componentsSeparatedByString:@","];
        _arrDS=(NSMutableArray *)arr;
        //[WalletStatusModel saveWalletKeyWord:self.keyWord];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark collectionview delageta datasource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _arrDS.count?:12;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
/** cell的内容*/
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SafWordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SafWordCell" forIndexPath:indexPath];
    NSString *str=[_arrDS objectAtIndex:indexPath.row];
    [cell.safeWordBtn setTitle:str forState:UIControlStateNormal];
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(21, 17, 10, 17);//分别为上、左、下、右
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
